<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layuiAdmin std - 通用后台管理模板系统（iframe标准版）</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">文章ID</label>
                    <div class="layui-input-inline">
                        <input type="text" name="id" placeholder="请输入文章ID" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">标题</label>
                    <div class="layui-input-inline">
                        <input type="text" name="title" placeholder="请输入文章标题" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">文章标签</label>
                    <div class="layui-input-inline">
                        <select name="label">
                            <option value="">请选择文章种类</option>
                            <c:forEach items="${types}" var="type">
                                <option value="${type.getId()}">${type.getTypeName()}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>
                <!--数据搜索功能提交按钮-->
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>
        <!--表单-->
        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-list" data-type="batchdel">删除</button>
                <button class="layui-btn layuiadmin-btn-list" data-type="add">添加</button>
            </div>
            <!--初始化表单入口-->
            <table id="demo0" lay-filter="LAY-app-content-list"></table>
            <!--是否发布的状态栏-->
            <script type="text/html" id="status">
                <input type="checkbox" disabled name="status" value="{{d.status}}" lay-skin="switch"
                       lay-text="发布|未发布"
                       lay-filter="status" {{ d.status== 1 ? 'checked' : '' }}>
            </script>
            <!--表单右部操作按钮-->
            <script type="text/html" id="tools">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<script src="${pageContext.request.contextPath}/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '${pageContext.request.contextPath}/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'contlist', 'table'], function () {
        var table = layui.table
            , form = layui.form;
        //初始化表格
        table.render({
            elem: '#demo0'
            , height: 400
            , url: '${pageContext.request.contextPath}/admin/blog' //数据接口
            , page: {//开启分页
                limit: 10, //每一页的条数
                limits: [5, 10, 15] //可以选择的显示条数
            }
            ,
            defaultToolbar: ['filter', 'print', 'exports']
            , title: '博客文章列表'
            , id: 'id' //这个就是表格重载的时候需要使用的字段
            , cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', title: 'ID', width: 80, sort: true, fixed: 'left'}
                , {field: 'typeId', title: '种类ID', width: 80}
                , {field: 'title', title: '博客题目', width: 230}
                , {field: 'content', title: '博客内容', width: 323}
                , {field: 'phTime', title: '创建时间', width: 190}
                , {field: 'number', title: '观看次数', width: 105, sort: true}
                , {field: 'image', title: '图片链接', width: 100}
                , {field: 'voteNumber', title: '评论数量', width: 120, sort: true}
                , {field: 'status', title: '是否发布', width: 120, templet: '#status'}
                , {fixed: 'right', title: '操作', toolbar: '#tools', width: 150}
            ]]
        });
        //监听行工具事件
        table.on('tool(LAY-app-content-list)', function (obj) {
            let data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除这篇博客吗', function (index) {
                    //数据持久化到数据库
                    $.post({
                        url: "${pageContext.request.contextPath}/admin/blog?method=del",
                        data: {
                            id: data.id
                        },
                        success: function (data) {
                            if (data === "true") {
                                obj.del();
                                layer.close(index);
                            } else {
                                layer.alert("删除失败")
                            }
                        }
                    })
                });
                //数据修改
            } else if (obj.event === 'edit') {
                layer.open({
                    type: 2
                    , content: '../edit/editBlog.jsp'
                    , area: ['550px', '550px']
                    , maxmin: true
                    //函数变量是固定的，不可以改变
                    , success: function (layero, index) {
                        var body = layer.getChildFrame('body', index);
                        //寻找数据填充地方
                        body.find('input[name=id]').val(data.id)
                        body.find('input[name=title]').val(data.title)
                        body.find('input[id=getContent]').val(data.content)
                        body.find('input[name=image]').val(data.image)
                        body.find('select[name=typeId]').val(data.typeId)
                        body.find('inpuit[name=status]').val(data.status)
                    }
                })
            }
        });
        //监听搜索
        form.on('submit(search)', function (data) {
            var field = data.field;
            table.reload('id', {
                //数据入口
                url: "${pageContext.request.contextPath}/admin/blog?method=search",
                where: {
                    data: JSON.stringify(field)
                }
            });
        });

        var $ = layui.$, active = {
            batchdel: function () {
                var checkStatus = table.checkStatus('id')
                    , checkData = checkStatus.data; //得到选中的数据

                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }
                layer.confirm('确定删除吗？', function (index) {
                    //使用ajax传递数据
                    $.ajax({
                        url: "${pageContext.request.contextPath}/admin/blog?method=dels",
                        type: 'post',
                        data: {
                            //传递的数据必须要是json字符串
                            blogs: JSON.stringify(checkData)
                        },
                        success: function (data) {
                            if (data === "true") {
                                table.reload('id');
                                layer.msg('已删除');
                            } else {
                                layer.alert("删除失败")
                            }
                        }
                    })
                });
            },
            add: function () {
                layer.open({
                    type: 2
                    , title: '添加文章'
                    , content: '../add/addBlog.jsp'
                    , maxmin: true
                    , area: ['550px', '550px']
                    , yes: function (index, layero) {
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                        submit.click();
                    }
                });
            }
        };
        $('.layui-btn.layuiadmin-btn-list').on('click', function () {
            let type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>
